using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using Microsoft.WindowsMobile.PocketOutlook;

using Facebook.Entity;

namespace DeviceEventSample
{
    public partial class EventsForm : Form
    {
        private OutlookSession os = new OutlookSession();

        public EventsForm()
        {
            InitializeComponent();
        }

        private void EventsForm_Load(object sender, EventArgs e)
        {
            //connect to facebook
            facebookService1.ApplicationKey = (string)Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\InTheHand\\FacebookDeviceSample", "ApplicationKey", "");
            facebookService1.Secret = (string)Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\InTheHand\\FacebookDeviceSample", "Secret", "");
            facebookService1.ConnectToFacebook();

            RefreshEventsList();
            
        }

        private void RefreshEventsList()
        {
            lvEvents.Items.Clear();
            ilEvents.Images.Clear();

            Cursor.Current = Cursors.WaitCursor;
            lvEvents.BeginUpdate();

            Collection<FacebookEvent> events = facebookService1.GetEvents();
            
            int i = 0;

            foreach (FacebookEvent e in events)
            {
                ListViewItem lvi = new ListViewItem(e.Name);
                lvi.SubItems.Add(e.StartDate.ToString());
                lvi.Tag = e;

                lvi.ImageIndex = i;
                ilEvents.Images.Add(e.Picture);
                i++;
       
                lvEvents.Items.Add(lvi);
            }

            lvEvents.EndUpdate();

            Cursor.Current = Cursors.Default;
        }

        private void lvEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvEvents.SelectedIndices.Count > 0 && lvEvents.SelectedIndices[0] > -1)
            {
                mnuAddToCalendar.Enabled = true;
            }
            else
            {
                mnuAddToCalendar.Enabled = false;
            }
        }

        private void mnuAddToCalendar_Click(object sender, EventArgs e)
        {
            if (lvEvents.SelectedIndices.Count > 0)
            {
                AddToCalendar();
            }
        }
        
        private void AddToCalendar()
        {
            
            foreach (int i in lvEvents.SelectedIndices)
            {
                if (i > -1)
                {
                    Appointment eventAppointment = null;

                    FacebookEvent fe = (FacebookEvent)lvEvents.Items[i].Tag;
                    Collection<EventUser> attendees = facebookService1.GetEventMembers(fe.EventId);

                    //check if it exists - if so update it
                    AppointmentCollection appointments = os.Appointments.Items.Restrict("[Categories] = \"Facebook\"");
                    
                    foreach (Appointment a in appointments)
                    {
                        if ((string)a.Properties["Facebook.EventId"] == fe.EventId)
                        {
                            eventAppointment = a;
                            break;
                        }
                    }

                    //if not found add a new entry    
                    if(eventAppointment == null)
                    {
                        eventAppointment = os.Appointments.Items.AddNew();
                        eventAppointment.Categories = "Facebook";
                        //add the event id in a custom property
                        eventAppointment.Properties.Add("Facebook.EventId", typeof(string));
                        eventAppointment.Properties["Facebook.EventId"] = fe.EventId;                        
                    }
                    
                    eventAppointment.Subject = fe.Name;
                    eventAppointment.Location = fe.Location;
                    eventAppointment.Body = fe.Description;
                    eventAppointment.Start = fe.StartDate;
                    eventAppointment.End = fe.EndDate;
                    RecipientCollection rc = eventAppointment.Recipients;
                    //clear current list
                    for (int irecipient = rc.Count - 1; irecipient > -1; irecipient--)
                    {
                        rc.Remove(irecipient);
                    }
                    foreach (EventUser eu in attendees)
                    {
                        if (eu.Attending == RSVPStatus.Attending)
                        {
                            //add recipient entries (no valid email addresses)
                            Recipient r = rc.Add(new Recipient(eu.User.Name, "facebook"));
                        }
                    }
                    eventAppointment.Update();
                } 
            }
        }

    }
}